define(function(require, exports, module) {

	var $ = require('jquery');
	require('../util/jquery.icheck');
	require('jquery/jquery-validation/1.11.0/jquery.validate');
	require('../util/jquery-validate-extends.js');
	require('jquery/jquery-validation/1.11.0/messages_zh');
	require('jquery/jquery-form/1.0.0/jquery.form.js');
	require('jquery/jquery-json/2.4.0/jquery.json.js');
	require('jbox');


	var url = $CONFIG.base_url + '/api/configure/get';
	$.get(url,function(data){
		var regist_photo_url = data.loginUrl;
		$(".regist-left").css("background-image","url("+regist_photo_url+")")
	});

	//正则表达式匹配函数
	exports.match=function(o,reg,tip){
		var patten = new RegExp(reg);
		if(patten.test($(o).val())){
			$(tip).attr({style:'display:block;'});
		}else{
			$(tip).attr({style:'display:none;'});
		};
	};
	//检测确认密码
	exports.matchRep=function(o,reg,tip){
		//console.log($(o).val());
		if($(o).val()==""){
			$(tip).attr({style:'display:none;'});
		}else if($(o).val()==$(reg).val()){
			$(tip).attr({style:'display:block;'});
		}else{
			$(tip).attr({style:'display:none;'});
		};
	};
	
	//为输入框注册事件
	exports.checkMail=function(o,reg,tip){
		$(o).keyup(function(){exports.match(o,reg,tip);});
		$(o).bind("paste",function(){exports.match(o,reg,tip);});
		$(o).blur(function(){exports.match(o,reg,tip);});		
	};
	//为确认输入框注册事件
	exports.checkMailRep=function(o,reg,tip){
		$(o).keyup(function(){exports.matchRep(o,reg,tip);});
		$(o).bind("paste",function(){exports.matchRep(o,reg,tip);});
		$(o).blur(function(){exports.matchRep(o,reg,tip);});		
	};
	
	$(".ok-img").attr({style:'display:none;'});
	
	exports.match("#name",/^[\u4e00-\u9fa5A-Za-z0-9-_][\S+]{0,30}$/,"#name-ok");
	exports.match("#loginName",/^[A-Za-z0-9][\\w\\-\\.]+@([\\w\\-]+\\.)+[\\w]{2,3}$/,"#loginName-ok");
	exports.match("#plainPassword",/^\w{6,18}$/,"#plainPassword-ok");
	exports.matchRep("#rePassword","#plainPassword","#rePassword-ok");
	exports.matchRep("#plainPassword","#rePassword","#rePassword-ok");
	
	$(document).ready(function() {
		// 为inputForm注册validate函数
		$("#regist-form").validate({
			rules : {
				name:{
					required:true,
					maxlength:20,
					remote:{
						url:"regist/check_name",
					},
				},
				loginName : {
					required : true,
					remote :{
						url:" regist/check_login_name",
					},
					ismobile : true
				},
				
				plainPassword : {
					required : true,
					minlength:6,
					maxlength:18
				},
				rePassword:{
					required : true,
					equalTo:"#plainPassword"
				}
			},
			messages : {
				name:{
					required:'请输入昵称',
					maxlength:'昵称最多20个字符<i class="icon-info-sign"></i>',
					remote:'昵称已经被注册'
				},
				loginName : {
					required : '请输入手机号',
					remote : '手机号已经被注册'
				},
				plainPassword : {
					required : '请输入密码',
					minlength: '密码最少6个字符',
					maxlength: '密码最多18个字符'
				},
				rePassword:{
					required : '请输入确认密码',
					equalTo:"请确认两次密码输入一致"
				}
			},
			errorContainer : "#error",
			errorLabelContainer:"#error ul",
			wrapper: "li"
		});
		$('.sel-group input').iCheck({
			radioClass : 'iradio_minimal-yellow',
			increaseArea : '-20%', // optional
		});
		
		$("#registUser").click(function(e){
			if(!$("#regist-form").valid()){
				return;
			}
			if ($("#servers").prop("checked")) {
				var data = $("#regist-form").serialize();
				var code = $("#confirm-code").val();
				var phone = $("#loginName").val();
				var formSubmit = $.post($CONFIG.base_url + "/regist",data);
				formSubmit.done(function(formReturn) {
					if(formReturn == false){
						$.jBox.tip("验证码不正确");
						return;
					}else{
					$.jBox.tip("注册成功！跳转登陆页面");
					setTimeout(function(){window.location.href = $CONFIG.base_url + "/home";},1000);
					}
				})
				.fail(function() {
					$.jBox.tip("注册失败，请重试");
				});
			}else{
				$.jBox.tip("您还没有同意我们的服务条款！");
				e.preventDefault();
			}
		});

		//获取验证码
		$("#code").click(function(e) {
			if($("#code").attr("disabled")=="disabled"){
				return;
			};
			if(!$("#loginName").valid()) return;
			var phone = $("#loginName").val();
			$.post($CONFIG.base_url + "/regist/text/send",{username:phone})
			.done(function(data){
					$(".code-btn").addClass("timer");					
					var loop;
					var t = 60;
					(function timer() {	
						$("#code").attr("disabled",true);						
						t = parseInt(t) - 1;
						if(t < 0) {
							clearTimeout(loop);
							$("#code").attr("disabled",false);
							$("#code").removeClass("timer");	
							$("#code").text("重新获取短信");
						}else{
							$("#code").text(t + " 秒后重新获取短信");
							loop = setTimeout(timer,1000);
						}
					})();
			})
			.fail(function() {
				$.jBox.tip("验证码获取失败");
			});
			//e.stopPropagation(); 
		});


		
		exports.checkMail("#name",/^[\u4e00-\u9fa5A-Za-z0-9-_][\S]{0,30}$/,"#name-ok");
		
		exports.checkMail("#loginName",/^0?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/,"#loginName-ok");
		
		exports.checkMail("#plainPassword",/^\w{6,18}$/,"#plainPassword-ok");

		exports.checkMailRep("#rePassword","#plainPassword","#rePassword-ok");
		
		exports.checkMailRep("#plainPassword","#rePassword","#rePassword-ok");
	});
});
